package by.bsu.fpmi.bbtp.impl;

import by.bsu.fpmi.bbtp.controllers.TransportCompanyController;
import by.bsu.fpmi.bbtp.dao.TransportCompanyDAO;
import by.bsu.fpmi.bbtp.entities.TransportCompany;
import by.bsu.fpmi.bbtp.entities.TransportCompanyHead;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;


@WebService(endpointInterface = "by.bsu.fpmi.bbtp.controllers.TransportCompanyController")
public class TransportCompanyControllerImpl implements TransportCompanyController {

    @Autowired
    private TransportCompanyDAO transportCompanyDAO;

    @Override
    public void saveTransportCompany(String sessionId, TransportCompany company) {
        transportCompanyDAO.save(company);
    }

    @Override
    public TransportCompany getTransportCompany(String sessionId, long companyId) {
        return transportCompanyDAO.find(companyId);
    }

    @Override
    public List<TransportCompanyHead> getTransportCompaniesList(String sessionId) {
        List<TransportCompany> companies = transportCompanyDAO.all();
        List<TransportCompanyHead> heads = new ArrayList<TransportCompanyHead>(companies.size());
        for (TransportCompany company : companies) {
            heads.add(company.getHead());
        }
        return heads;
    }

    @Override
    public void deleteTransportCompany(String sessionId, long companyId) {
        TransportCompany company = transportCompanyDAO.find(companyId);
        transportCompanyDAO.remove(company);
    }
}
